package com.tuoyu.peopletest.adapter;//package com.stresscude.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tuoyu.peopletest.R;
import com.tuoyu.peopletest.common.Constant;
import com.tuoyu.peopletest.entity.DataBean;
import com.tuoyu.peopletest.scinterface.OnItemDataListener;
import com.tuoyu.peopletest.utils.SCTimeUtils;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 用户测试结果列表
 */
public class PDTestListAdapter extends RecyclerView.Adapter<PDTestListAdapter.MyMCHolder> {
    public String TAG = this.getClass().getSimpleName();
    public OnItemDataListener<DataBean> mListener;

    private List<DataBean> mDatas;
    private Context mContext;
    private boolean isOnClick;


    public PDTestListAdapter(Context mContext, List<DataBean> mDatas, boolean isOnClick) {
        this.mDatas = mDatas;
        this.mContext = mContext;
        this.isOnClick = isOnClick;
    }


    @Override
    public MyMCHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hcy_test_list, null);
        MyMCHolder holder = new MyMCHolder(itemView);

        if (isOnClick) {
            holder.itemView.setOnClickListener((v) -> {
                int position = (int) v.getTag(R.id.sc_id_recycleview_item);
                if (mListener != null) {
                    mListener.onItemData(mDatas.get(position), position);
                }

            });
        }

        return holder;
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public void onBindViewHolder(MyMCHolder holder, int position) {
        holder.itemView.setTag(R.id.sc_id_recycleview_item, position);
        DataBean bean = mDatas.get(position);
        if (position % 2 == 0) {
            holder.vItemMain.setBackgroundResource(R.color.white);
        } else {
            holder.vItemMain.setBackgroundResource(R.color.sc_c_bg_gray_d8_39);
        }

         //血浆同型\n半胱氨酸水平
         holder.tvDate.setText(SCTimeUtils.getFormatData12(bean.time));
         holder.tvTime.setText(SCTimeUtils.getFormatData09_01(bean.time));
         holder.tvNumber.setText(bean.number);
//         holder.tvValue.setText(""+bean.area1+"\n"+bean.area2);
        holder.tvValue.setText(bean.result);

        holder.tvArea1.setText(bean.area1);
        holder.tvArea2.setText(bean.area2);

    }

    public void setItemClickListener(OnItemDataListener<DataBean> listener) {
        this.mListener = listener;
    }

    public void addDatas(List<DataBean> list) {
        if (list != null) {
            mDatas.addAll(list);
        }
    }

    public void upDatas(List<DataBean> list) {
        mDatas.clear();
        if (list != null) {
            mDatas.addAll(list);
        }

        notifyDataSetChanged();
    }

    static class MyMCHolder extends RecyclerView.ViewHolder {
        public View vItemMain;
        public TextView tvDate;
        public TextView tvTime;
        public TextView tvNumber;
        public TextView tvValue;
        public TextView tvArea1;
        public TextView tvArea2;

        public MyMCHolder(@NonNull View itemView) {
            super(itemView);
            vItemMain = itemView.findViewById(R.id.v_item_main);
            tvDate = itemView.findViewById(R.id.v_title_data);


            tvTime = itemView.findViewById(R.id.v_title_time);

            tvNumber = itemView.findViewById(R.id.v_title_number);
            tvValue = itemView.findViewById(R.id.v_title_value);

            tvArea1 = itemView.findViewById(R.id.v_title_area1);
            tvArea2 = itemView.findViewById(R.id.v_title_area2);

            if(Constant.isShowArea) {
                tvArea1.setVisibility(View.VISIBLE);
                tvArea2.setVisibility(View.VISIBLE);
            }else{
                tvArea1.setVisibility(View.GONE);
                tvArea2.setVisibility(View.GONE);
            }
        }
    }
}
